/*
 * Middleware
 * Copyright (C) 2010/11
 * Marco Buzzanca, Davide Giuseppe Monaco, Riccardo Pulvirenti, Giuseppe Ravidà,
 * Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: xml_syn_logging_test.hpp
 */

#ifndef _XML_SYN_LOGGING_TEST_HPP_
#define _XML_SYN_LOGGING_TEST_HPP_

#include <iostream>
#include <string>
#include <boost/thread.hpp>
#include <boost/bind.hpp>

#include "xml_syn_logging.hpp"
#include "xml_incapsulator.hpp"
#include "address.hpp"
#include "task_descriptor.hpp"
#include "worker_descriptor.hpp"
#include "workflow.hpp"

namespace middleware {
//-----------------------------------------------------------------------------
// Definitions
void XmlSynLoggerTest();
void XmlSynLoggerTest_Event(logging::XmlSynLogger& xsl);
void XmlSynLoggerTest_WorkflowSubmitted(logging::XmlSynLogger& xsl);
void XmlSynLoggerTest_WorkflowEnqueued(logging::XmlSynLogger& xsl);
void XmlSynLoggerTest_WorkflowDequeued(logging::XmlSynLogger& xsl);
void XmlSynLoggerTest_TaskSent(logging::XmlSynLogger& xsl);
void XmlSynLoggerTest_TaskReceived(logging::XmlSynLogger& xsl);
void XmlSynLoggerTest_WorkerDescriptorAdded(logging::XmlSynLogger& xsl);
void XmlSynLoggerTest_WorkerDescriptorUpdated(logging::XmlSynLogger& xsl);
void XmlSynLoggerTest_TaskEnqueued(logging::XmlSynLogger& xsl);
void XmlSynLoggerTest_TaskDequeued(logging::XmlSynLogger& xsl);
void XmlSynLoggerTest_StatusUpdateLoop(logging::XmlSynLogger& xsl);
void XmlSynLoggerTest_TDCCollectorLoopExec(logging::XmlSynLogger& xsl);
void XmlSynLoggerTest_ExecTaskStillNotExecuted(logging::XmlSynLogger& xsl);
void XmlSynLoggerTest_TaskExecuted(logging::XmlSynLogger& xsl);
void XmlSynLoggerTest_WorkerDescriptorSent(logging::XmlSynLogger& xsl);
void XmlSynLoggerTest_XI();
//-----------------------------------------------------------------------------
// Implementations
void XmlSynLoggerTest() {
	using namespace logging;
	using namespace xml;
	std::cout << "Creating XmlSynLogger object" << std::endl;
	XmlSynLogger mylogger("xmllog_test", "peer_test", kLogOpenOverwriteFile,
			kAddXsl);
	// Waiting
	std::cout << "Sleeping..." << std::endl;
	boost::this_thread::sleep(boost::posix_time::milliseconds(7000));
	XmlSynLoggerTest_Event(mylogger);
	XmlSynLoggerTest_WorkflowSubmitted(mylogger);
	XmlSynLoggerTest_WorkflowEnqueued(mylogger);
	XmlSynLoggerTest_WorkflowDequeued(mylogger);
	XmlSynLoggerTest_TaskSent(mylogger);
	XmlSynLoggerTest_TaskReceived(mylogger);
	XmlSynLoggerTest_WorkerDescriptorAdded(mylogger);
	XmlSynLoggerTest_WorkerDescriptorUpdated(mylogger);
	XmlSynLoggerTest_TaskEnqueued(mylogger);
	XmlSynLoggerTest_TaskDequeued(mylogger);
	XmlSynLoggerTest_StatusUpdateLoop(mylogger);
	XmlSynLoggerTest_TDCCollectorLoopExec(mylogger);
	XmlSynLoggerTest_ExecTaskStillNotExecuted(mylogger);
	XmlSynLoggerTest_TaskExecuted(mylogger);
	XmlSynLoggerTest_WorkerDescriptorSent(mylogger);
	boost::this_thread::sleep(boost::posix_time::milliseconds(100000));
}
void XmlSynLoggerTest_Event(logging::XmlSynLogger& xsl) {
	using namespace logging;
	using namespace xml;
	std::cout << "Adding an Event..." << std::endl;
	Event e("Test", "This is the body");
	xsl << e;
	std::cout << "Event ADDED!" << std::endl;
	boost::this_thread::sleep(boost::posix_time::milliseconds(7000));
}
void XmlSynLoggerTest_WorkflowSubmitted(logging::XmlSynLogger& xsl) {
	using namespace logging;
	using namespace xml;
	std::cout << "Adding a WorkflowSubmitted..." << std::endl;
	TaskDescriptor td1, td2, td3;
	Workflow wf;
	wf.push_back(td1); wf.push_back(td2); wf.push_back(td3);
	WorkflowSubmitted e(&wf, "Test");
	xsl << e;
	std::cout << "WorkflowSubmitted ADDED!" << std::endl;
	boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
}
void XmlSynLoggerTest_WorkflowEnqueued(logging::XmlSynLogger& xsl) {
	using namespace logging;
	using namespace xml;
	std::cout << "Adding a WorkflowEnqueued..." << std::endl;
	TaskDescriptor td1, td2, td3;
	Workflow wf;
	wf.push_back(td1); wf.push_back(td2); wf.push_back(td3);
	WorkflowEnqueued e(&wf, "Test");
	xsl << e;
	std::cout << "WorkflowEnqueued ADDED!" << std::endl;
	boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
}
void XmlSynLoggerTest_WorkflowDequeued(logging::XmlSynLogger& xsl) {
	using namespace logging;
	using namespace xml;
	std::cout << "Adding a WorkflowDequeued..." << std::endl;
	TaskDescriptor td1, td2, td3;
	Workflow wf;
	wf.push_back(td1); wf.push_back(td2); wf.push_back(td3);
	WorkflowDequeued e(&wf, "Test");
	xsl << e;
	std::cout << "WorkflowDequeued ADDED!" << std::endl;
	boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
}
void XmlSynLoggerTest_TaskSent(logging::XmlSynLogger& xsl) {
	using namespace logging;
	using namespace xml;
	std::cout << "Adding a TaskSent..." << std::endl;
	TaskDescriptor td;
	Address ad;
	ad.set_ip("127.0.0.1");
	ad.set_port(1234); ad.set_port_disc(1235); ad.set_port_w(1236);
	TaskSent e(&td, &ad, "Test");
	xsl << e;
	std::cout << "TaskSent ADDED!" << std::endl;
	boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
}
void XmlSynLoggerTest_TaskReceived(logging::XmlSynLogger& xsl) {
	using namespace logging;
	using namespace xml;
	std::cout << "Adding a TaskReceived..." << std::endl;
	TaskDescriptor td;
	Address ad;
	ad.set_ip("127.0.0.1");
	ad.set_port(1234); ad.set_port_disc(1235); ad.set_port_w(1236);
	TaskReceived e(&td, &ad, "Test");
	xsl << e;
	std::cout << "TaskReceived ADDED!" << std::endl;
	boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
}
void XmlSynLoggerTest_WorkerDescriptorAdded(logging::XmlSynLogger& xsl) {
	using namespace logging;
	using namespace xml;
	std::cout << "Adding a WorkerDescriptorAdded..." << std::endl;
	WorkerDescriptor wd;
	Address ad;
	ad.set_ip("127.0.0.1");
	ad.set_port(1234); ad.set_port_disc(1235); ad.set_port_w(1236);
	WorkerDescriptorAdded e(&wd, &ad, "Test");
	xsl << e;
	std::cout << "WorkerDescriptorAdded ADDED!" << std::endl;
	boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
}
void XmlSynLoggerTest_WorkerDescriptorUpdated(logging::XmlSynLogger& xsl) {
	using namespace logging;
	using namespace xml;
	std::cout << "Adding a WorkerDescriptorUpdated..." << std::endl;
	WorkerDescriptor wd;
	Address ad;
	ad.set_ip("127.0.0.1");
	ad.set_port(1234); ad.set_port_disc(1235); ad.set_port_w(1236);
	WorkerDescriptorUpdated e(&wd, &ad, "Test");
	xsl << e;
	std::cout << "WorkerDescriptorUpdated ADDED!" << std::endl;
	boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
}
void XmlSynLoggerTest_TaskEnqueued(logging::XmlSynLogger& xsl) {
	using namespace logging;
	using namespace xml;
	std::cout << "Adding a TaskEnqueued..." << std::endl;
	TaskDescriptor td;
	TaskEnqueued e(&td, "Test");
	xsl << e;
	std::cout << "TaskEnqueued ADDED!" << std::endl;
	boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
}
void XmlSynLoggerTest_TaskDequeued(logging::XmlSynLogger& xsl) {
	using namespace logging;
	using namespace xml;
	std::cout << "Adding a TaskDequeued..." << std::endl;
	TaskDescriptor td;
	TaskDequeued e(&td, "Test");
	xsl << e;
	std::cout << "TaskDequeued ADDED!" << std::endl;
	boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
}
void XmlSynLoggerTest_StatusUpdateLoop(logging::XmlSynLogger& xsl) {
	using namespace logging;
	using namespace xml;
	std::cout << "Adding a StatusUpdateLoop..." << std::endl;
	WorkerDescriptor wd1, wd2;
	StatusUpdateLoop e(false, &wd1, &wd2, "Test");
	xsl << e;
	std::cout << "StatusUpdateLoop ADDED!" << std::endl;
	boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
}
void XmlSynLoggerTest_TDCCollectorLoopExec(logging::XmlSynLogger& xsl) {
	using namespace logging;
	using namespace xml;
	std::cout << "Adding a TDCCollectorLoopExec..." << std::endl;
	TDCCollectorLoopExec e(0, "Test");
	xsl << e;
	std::cout << "TDCCollectorLoopExec ADDED!" << std::endl;
	boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
}
void XmlSynLoggerTest_ExecTaskStillNotExecuted(logging::XmlSynLogger& xsl) {
	using namespace logging;
	using namespace xml;
	std::cout << "Adding a ExecTaskStillNotExecuted..." << std::endl;
	TaskDescriptor td;
	ExecTaskStillNotExecuted e(&td, 0, 0, "Test");
	xsl << e;
	std::cout << "ExecTaskStillNotExecuted ADDED!" << std::endl;
	boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
}
void XmlSynLoggerTest_TaskExecuted(logging::XmlSynLogger& xsl) {
	using namespace logging;
	using namespace xml;
	std::cout << "Adding a TaskExecuted..." << std::endl;
	TaskDescriptor td;
	TaskExecuted e(&td, "Test");
	xsl << e;
	std::cout << "TaskExecuted ADDED!" << std::endl;
	boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
}
void XmlSynLoggerTest_WorkerDescriptorSent(logging::XmlSynLogger& xsl) {
	using namespace logging;
	using namespace xml;
	std::cout << "Adding a WorkerDescriptorSent..." << std::endl;
	WorkerDescriptor wd;
	Address ad;
	ad.set_ip("127.0.0.1");
	ad.set_port(1234); ad.set_port_disc(1235); ad.set_port_w(1236);
	WorkerDescriptorSent e(&wd, &ad, "Test");
	xsl << e;
	std::cout << "WorkerDescriptorSent ADDED!" << std::endl;
	boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
}
void XmlSynLoggerTest_XI() {
	using namespace logging;
	using namespace xml;
	TaskDescriptor td;
	Address ad("127.0.0.1", 1234, 1235, 1236);
	WorkerDescriptor wd;
	XmlIncapsulator xi, xii, xiii;
	xi << td;
	xii << ad;
	xiii << wd;
	std::cout << xi.GetXml() << std::endl;
	std::cout << xii.GetXml() << std::endl;
	std::cout << xiii.GetXml() << std::endl;
}
} /* middleware */

#endif
